package com.pagoda.nerp.trade.service.impl;

import com.pagoda.nerp.trade.dao.AccRoleDao;
import com.pagoda.nerp.trade.dao.AccUserRoleDao;
import com.pagoda.nerp.trade.dao.AccUsersDao;
import com.pagoda.nerp.trade.dao.SysBillRuleDao;
import com.pagoda.nerp.trade.entity.AccUserRole;
import com.pagoda.nerp.trade.entity.AccUsers;
import com.pagoda.nerp.trade.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dell on 2016/11/22.
 */
@Service
public class UserServiceImpl implements UserService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AccUsersDao userDao;
    @Autowired
    private AccUserRoleDao userRoleDao;
    @Autowired
    private SysBillRuleDao sysBillRuleDao;

    @Autowired
    private AccRoleDao roleDao;


    public AccUsers queryAccUserInfoByUserId(int userId) {
        return userDao.queryAccUserInfoByUserId(userId);
    }

    public AccUsers queryAccUser(Map<String,String> map) {
        return userDao.queryAccUser(map);
    }


    /**
     * 查询用户列表
     *
     * @param map
     * @return
     */
    public List<AccUsers> queryAccUserList(Map<String,String> map) {
        List<AccUsers> usersInfos = null;

        if (map.get("roleId") != null) {
            //首先通过roleId查询
            usersInfos = userDao.queryUserInfoListByRoleId(Integer.parseInt(map.get("roleId")));
        } else if (map.get("roleId") == null && map.get("roleCode") != null) {
            //通过roleCode查询
            usersInfos = userDao.queryUserInfoListByRoleCode(map.get("roleCode"));
        } else if (map.get("roleId") == null && map.get("roleCode") == null) {
            //查询全部
            usersInfos = userDao.queryAll();
        }

        return usersInfos;
    }


    /**
     * 删除用户（支持批量删除）
     *
     * @param userIdList
     * @return
     */
    @Transactional
    public int deleteUsers(List<Integer> userIdList) {
        return userDao.deleteUsers(userIdList);
    }

    /**
     * 新增用户,成功返回主键
     * @param accUsers
     * @return
     */
    @Transactional
    public int addUser(AccUsers accUsers)  {

//        String userCode= sysBillRuleDao.generateCode("USER_CODE");
//        if (userCode == null) {
//            logger.error(" 获取UserCode异常!");
//            throw new BaseException(ErrorCodeMappingEnums.FAIL);
//        }
//        accUsers.setUserCode(userCode);

        return userDao.insert(accUsers);

    }

    /**
     * 修改用户
     * @param accUsers
     * @return
     */
    @Transactional
    public int updateUser(AccUsers accUsers) {

        return userDao.updateByPrimaryKeySelective(accUsers);
    }

    /**
     * 绑定用户角色
     *
     * @return
     */
    @Transactional
    public int bindUserAndRole(Map<String, String> map) {

        String[] userIdArr = map.get("userIds").split(",");
        String[] roleIdArr = map.get("roleIds").split(",");
        List userIdList = Arrays.asList(userIdArr);
        List roleIdList = Arrays.asList(roleIdArr);
        Map<String, List> paramMap = new HashMap<String, List>();

        paramMap.put("userIdList",userIdList);
        paramMap.put("roleIdList",roleIdList);

        return userRoleDao.insert(paramMap);
    }

}



